package boardgamer.common.client.preference;

import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import boardgamer.yavalath.R;
import com.google.common.annotations.VisibleForTesting;

/**
 * @author garysoed
 */
public class IconListPreference extends ListPreference {

  @VisibleForTesting ImageView iconView;
  private Integer iconResource = null;

  public IconListPreference(Context context) {
    super(context);
    this.setWidgetLayoutResource(R.layout.preference_widget_icon);
  }
  
  public IconListPreference(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    this.setWidgetLayoutResource(R.layout.preference_widget_icon);
  }
  
  public void setIconViewResource(int resource) {
    this.iconResource = resource;
    if (iconView != null) {
      iconView.setImageResource(iconResource);
    }
  }

  @Override
  protected void onBindView(View view) {
    super.onBindView(view);
    this.iconView = (ImageView) view.findViewById(R.id.icon);
    if (iconResource != null) {
      iconView.setImageResource(iconResource);
    }
  }
  
  @VisibleForTesting void bindView(View view) {
    onBindView(view);
  }
}
